/**
 * 非空判断工具
 */

///判断属性是否不为空
export const isNotEmpty = <T>(value : T) : boolean => {
	if (value === null || value === undefined) {
		return false;
	}
	if (typeof value === 'string' && value.trim() === '') {
		return false;
	}
	if (Array.isArray(value) && value.length === 0) {
		return false;
	}
	return true;
}

export const isEmpty = <T>(value : T) : boolean => {
	if (value === null || value === undefined) {
		return true;
	}
	if (typeof value === 'string' && value.trim() === '') {
		return true;
	}
	if (Array.isArray(value) && value.length === 0) {
		return true;
	}
	return false;
}

export const validateNull = (val : any) => {
	if (typeof val === 'boolean') {
		return false;
	}
	if (typeof val === 'number') {
		return false;
	}
	if (val instanceof Array) {
		if (val.length === 0) return true;
	} else if (val instanceof Object) {
		if (JSON.stringify(val) === '{}') return true;
	} else {
		if (val === 'null' || val == null || val === 'undefined' || val === undefined || val === '') return true;
		return false;
	}
	return false;
};

export function cloneObject(obj : any) {
	return JSON.parse(JSON.stringify(obj));
}

export const isNoMore = (dataList : Array<any>, pageSize : number = 10) => {
	return isEmpty(dataList) || dataList.length < pageSize
}

export const toCamelCase = (str : string) => {
	return str.replace(/_([a-z])/g, (g) => g[1].toUpperCase())
}

export const convertKeysToCamelCase = (obj : any) => {
	if (Array.isArray(obj)) {
		return obj.map(item => convertKeysToCamelCase(item));
	} else if (obj !== null && typeof obj === 'object') {
		return Object.keys(obj).reduce((acc, key) => {
			const camelKey = toCamelCase(key);
			acc[camelKey] = convertKeysToCamelCase(obj[key]);
			return acc;
		}, {});
	}
	return obj;
}